﻿using System;
using System.Data.SqlClient;
using System.Configuration;

namespace Wild2.Models.Auth
{
    public class EMail
    {
        public static String GetMail(Int32 userId)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT TOP(1) [EMail] FROM [Users] WHERE [ID] = @UID", conn))
                {
                    cmd.Parameters.Add("@UID", System.Data.SqlDbType.Int).Value = userId;
                    cmd.Connection.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (reader["Email"] != DBNull.Value)
                                return reader["Email"].ToString();
                        }
                        reader.Close();
                    }
                    cmd.Connection.Close();
                }
            }
            return null;
        }

        public static bool SetMail(Int32 userId, String mail)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("UPDATE Users SET [EMail] = @Mail WHERE ([ID] = @UID) AND ([EMail] IS NULL)", conn))
                {
                    cmd.Parameters.Add("@UID", System.Data.SqlDbType.Int).Value = userId;
                    if (String.IsNullOrEmpty(mail)) cmd.Parameters.Add("@Mail", System.Data.SqlDbType.NVarChar, 100).Value = DBNull.Value;
                    else cmd.Parameters.Add("@Mail", System.Data.SqlDbType.NVarChar, 100).Value = mail;
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();
                }
            }
            return true;
        }

        public static bool ChangeMail(Int32 userId, String mail, String password)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT TOP(1) COUNT(ID) FROM [Users] WHERE ([ID] = @UID) AND ([Password] = @Pass)", conn))
                {
                    cmd.Parameters.Add("@UID", System.Data.SqlDbType.Int).Value = userId;
                    cmd.Parameters.Add("@Pass", System.Data.SqlDbType.NVarChar, 50).Value = password;
                    cmd.Connection.Open();
                    if (Convert.ToInt32(cmd.ExecuteScalar()) == 0) return false;
                    cmd.Connection.Close();
                }
                using (SqlCommand cmd = new SqlCommand("UPDATE Users SET [EMail] = @Mail WHERE ([ID] = @UID) AND ([Password] = @Pass)", conn))
                {
                    cmd.Parameters.Add("@UID", System.Data.SqlDbType.Int).Value = userId;
                    cmd.Parameters.Add("@Pass", System.Data.SqlDbType.NVarChar, 50).Value = password;
                    if (String.IsNullOrEmpty(mail)) cmd.Parameters.Add("@Mail", System.Data.SqlDbType.NVarChar, 100).Value = DBNull.Value;
                    else cmd.Parameters.Add("@Mail", System.Data.SqlDbType.NVarChar, 100).Value = mail;
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();
                }
            }
            return true;
        }
    }
}